 package coffeebar;

import java.sql.*;
import java.util.*;
import javax.swing.*;

public class Controller {
	static CurrentTransaction currentTransaction;
	static LinkedList pastTransactions;
	static LinkedList items;
	static GUI gui;

	static Timestamp from;
	static Timestamp to;
	static LinkedList users;
	static LinkedList loginUsers;
	static JFrame window;
	static int sessionID = 0;

	static User currentUser;

	public static void main(String[] args) {
		// create empty current transaction
		currentTransaction = new CurrentTransaction();

		from = new Timestamp(System.currentTimeMillis());
		to = new Timestamp(System.currentTimeMillis());

		try {
			TalkToDB.connect();
			// create list of past transactions
			loadTransactions();

			loginUsers = TalkToDB.getUsers(true);	
			// TalkToDB.disconnect();
		} catch (SQLException ex) {
			System.out.println(ex.getMessage());
			System.out.println("We are screwed, db is likely down. " +
					"Set TalkToDB.dbHost to dnsmamma.dyndns.org if it is not.");
			//TODO make message box with instructions
			System.exit(1); //FIXME check exit code
		} catch (Exception e) {
			System.out.println(e.getMessage());
			// System.exit(0);
		}

		// make gui
		gui = new GUI();

		loginScreen();
		// gui.showMainWin();
		// cashout();

	}

	public static void loadTransactions() {
		try {
			// create list of past transactions
			to = new Timestamp(System.currentTimeMillis());
			pastTransactions = TalkToDB.getAllTransaction(from, to);
		} catch (Exception ex) {
			System.out.println("loadTransaction error: " + ex.getMessage());
		}
	}

	public static GUI getGUI() {
		return gui;
	}

	public static JFrame getWindow() {
		return window;
	}

	public static Transaction getCurrentTransaction() {
		return currentTransaction;
	}

	public static void submitTransaction() {
		// currentTransaction.setTimestamp(System.currentTimeMillis());
		currentTransaction.setUser(currentUser);
		try {
			currentTransaction.setTimestamp(new Timestamp(System
					.currentTimeMillis()));
			TalkToDB.writeTransaction(currentTransaction);
			loadTransactions();
		} catch (Exception e) {
			System.out.println("Controller:SubmitTransaction:"+ e.getMessage());
		}
		System.out.println("this is where it saved in db");
		cancelTransaction();
	}

	public static void cancelTransaction() {
		currentTransaction = new CurrentTransaction();
		getGUI().update();
	}

	public static void cancelLastTransaction() {
		try {
			TalkToDB.cancelLastTransaction();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void cashout() {
		System.out.println("This is where cashout happens");
		to = new Timestamp(System.currentTimeMillis());
		System.out.println("From: " + from);
		System.out.println("To: " + to);

		try {
			GUI.setCORealTotal(TalkToDB.calculateTotal(Controller.from,
					Controller.to) + TalkToDB.cashLeft());
			System.out.println(GUI.cashout.moneyCountingArea.realTotal);
		} catch (Exception e) {
			System.out.println("BIG MISTAKE1 " + e.getMessage());
		}
		gui.showLogoutWin();

	}

	public static void doLogin(User u) {
		currentUser = u;
		try {
			sessionID = TalkToDB.newTillLogin(currentUser.id, new Timestamp(
					System.currentTimeMillis()), 0);
		} catch (SQLException e) {
			System.out.println("Arse, new session failed: " + e.getMessage());
		}
		System.out.println("New session created for user " + u.toString());
	}

	public static void loginScreen() {
		try {
			gui.setCIExpectedTotal(TalkToDB.cashLeft());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		gui.showLoginWin();
	}

	public static void recordMoney(double total) {
		// FIXME: dirty dirty dirty hack
		if (GUI.loginWin != null && GUI.loginWin.isVisible()) { // we are login
			// in
			try {
				TalkToDB.updateCashIn(sessionID, total);
				// create list of items
				items = TalkToDB.getAllItems();
				// create list of users (including promotions)
				users = TalkToDB.getUsers(false);

				// add items and transactions to gui
				gui.addTransactions();
				gui.addItems(items);
				gui.addUsers(users);

				// we managed to login, now display the main POS
				gui.showMainWin();
			} catch (SQLException e) {
				System.out.println("Record money: " + e.getMessage());
			}
		} else { // we are login out
			try {
				TalkToDB.cashoutTill(sessionID, total, new Timestamp(System
						.currentTimeMillis()));
				GUI.cashout.gotoQuestion1();
			} catch (SQLException e) {
				System.out.println("Cashout record money error: "
						+ e.getMessage());
			}
		}
	}

	public static void quit() {
		try {
			TalkToDB.disconnect();
		} catch (SQLException e) {
			// FIXME Auto-generated catch block
			e.printStackTrace();
		}
		System.exit(0);
	}

	public static Object[][] getTransactionArray() {
		Transaction t = getCurrentTransaction();
		Object[] a= t.getTransactions();
		Object[][] o = new Object[a.length][3];
		for (int i=0;i<a.length;i++){
			o[i] = ((Item)a[i]).toArray();
		}
		
		return o;
	}
}
